<template>
  <!-- <el-button
    type="primary"
    plain
    icon="Plus"
    @click="handleAdd"
    >新增</el-button
  > -->
  <!-- <el-button type="success" plain icon="Check" @click="handleCopy"
    >复制上月</el-button
  > -->
  
  <div class="home-box" style="margin-left: 10px">
    <el-form :inline="true" :model="PageForm">
      <el-form-item label="部门名称">
        <el-input v-model="PageForm.deptName" placeholder="部门名称" clearable/>
      </el-form-item>
      <el-form-item label="岗位名称">
        <el-input
          v-model="PageForm.positionName"
          placeholder="岗位名称"
          clearable
        ></el-input>
      </el-form-item>
	  <el-form-item label="岗位考核条目">
	    <el-input
	      v-model="PageForm.target"
	      placeholder="岗位考核条目"
	      clearable
	    ></el-input>
	  </el-form-item>
	  
  	
      <el-form-item>
        <el-button @click="searchEvent" type="primary">查询 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="update" type="primary">重置 </el-button>
      </el-form-item>
    </el-form>
  </div>
  
  
  <el-button color="#626aef" :dark="isDark" plain icon="Upload" @click="handleUP"
  v-auth="'导入关联业绩'"
    >导入</el-button
  >
  
  <el-button
    color="#626aef"
    style="margin-left: 10px"
    :dark="isDark"
    plain
    icon="Download"
    @click="handleDown"
	v-auth="'导出关联业绩'"
    >导出</el-button
  >
  
  
  <el-tooltip class="item" effect="dark" content="刷新" placement="top">
    <el-button circle icon="Refresh" @click="update" />
  </el-tooltip>
  
  <el-table
    :data="PerformanceRuleList"
    border
    style="width: 100%; margin: 20px 0px; border-radius: 8px;box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);"
    :header-cell-style="{
      background: '#337ecc !important',
      color: '#ffffff',
      border: 'none !important',
      textAlign: 'center',
    }"
    :span-method="(value) => rowSpanRulesHandler(value, PerformanceRuleList)"
  >
  
  
  <el-table-column label="部门/岗位" width="180">
    <template #default="scope">
      <div style="display: flex; align-items: center">
        <span style="margin-left: 10px">{{ scope.row.deptName }}/{{ scope.row.position }}</span>
      </div>
    </template>
  </el-table-column>
  
  <el-table-column label="岗位考核条目" width="180">
    <template #default="scope">
      <div style="display: flex; align-items: center">
        <span style="margin-left: 10px">{{ scope.row.target2 == null ? "仅用于评分结算":scope.row.target2}}</span>
      </div>
    </template>
  </el-table-column>
  
  <el-table-column label="计算方式" width="180">
    <template #default="scope">
      <div style="display: flex; align-items: center">
        <span style="margin-left: 10px">{{ scope.row.type }}</span>  

      </div>
    </template>
  </el-table-column>
  
  <el-table-column label="关联部门具体考核指标" width="210">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px">{{ scope.row.deptNamePerformance}}/{{scope.row.type}}/{{scope.row.target}}</span>
        </div>
      </template>
    </el-table-column>
	
	<el-table-column prop="budget" label="预算值" width=""></el-table-column>
	<el-table-column prop="complete" label="完成值" width=""></el-table-column>
	
	<el-table-column label="实际完成进度" width="">
		<template #default="scope">
		  <div style="display: flex; align-items: center">
		    <span style="margin-left: 10px">{{ scope.row.grade1}}%</span>
		  </div>
		</template>
	</el-table-column>
	
	
	<el-table-column prop="grade2" label="转换得分" width=""></el-table-column>
	
	<el-table-column prop="settlement" label="是否应用评分结算" width="">
		<template #default="scope">
		  <div style="display: flex; align-items: center">
		    <span style="margin-left: 10px">{{ scope.row.settlement == 0?"否":"是"}}</span>
		  </div>
		</template>
	</el-table-column>
	
	<el-table-column prop="ins" label="备注"/>
	
	<el-table-column label="最后更新时间" width="200">
	  <template #default="scope">
	    <div style="display: flex; align-items: center">
	      <el-icon><timer/></el-icon>
	      <span style="margin-left: 10px">{{ scope.row.updateTime }}</span>
	    </div>
	  </template>
	</el-table-column>
	
    
    
    <el-table-column label="操作" width="150">
      <template #default="scope">
        <!-- <el-button size="small" @click="handleEdit(scope.$index, scope.row)" 
          >编辑</el-button
        > -->
        <el-button
          size="small"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <pagination
    class="pagination"
    v-show="PerformanceListPage.total > 0"
    :total="PerformanceListPage.total"
    v-model:page="PageForm.page"
    v-model:limit="PageForm.page_size"
    @pagination="handlePagination"
  />
  <Dialog
    v-model="dialogFormVisible"
    :CadreList="CadreList"
    :close="close"
    :update="update"
    :selectContent="selectContent"
    :type="type"
    :title="title"
  ></Dialog>
  <FormDialog
    v-model="dialog"
    :close="closeForm"
    :update="update"
  ></FormDialog>
  <UploadExcel
        v-model="upload.open"
        :title="upload.upLoadTitle"
        :url="importUrl"
        :tipShow="true"
        @onSuccess="onSuccess"
        @onDownTemplate="onDownTemplate"
      ></UploadExcel>
	  
	  
	  
</template>
<script setup>
import {
  ref,
  onMounted,
  onBeforeMount,
  reactive,
  defineEmits,
  watch,
} from "vue";
//修改
import { getRulePage, deletetRule, updatetRule,copy,search } from "@/api/positionPerformance";
//修改
import Dialog from "./performanceDialog";
//修改
import FormDialog from "./performanceForm";

import { ElMessage } from "element-plus";
import {getEmpPerformanceTemplate} from "@/api/template";
import UploadExcel from "@/components/SendAndDownload/index";
import {downLoadPositionPerformance} from "@/api/downExcel";

const PageForm = ref({
	deptName:"",
	positionName:"",
	target:"",
  page: 1,
  page_size: 10,
});

const PerformanceListPage = ref([]);
const PerformanceRuleList = ref([]);
onBeforeMount(() => {
 getRulePage(PageForm.value).then(data=>{
     PerformanceListPage.value = data.data;
     PerformanceRuleList.value = data.data.records;
 })
});

const searchEvent = () => {
  search(PageForm.value).then((data) => {
     PerformanceListPage.value = data.data;
     PerformanceRuleList.value = data.data.records;
  });
};


watch(
  PerformanceRuleList,
  (newVal) => {
  },
  { deep: true }
);

const handlePagination=(value)=>{
	
	if (PageForm.value.deptName == "" && PageForm.value.positionName == ""&& PageForm.value.target == "") {
	  getRulePage(PageForm.value).then(data=>{
	      PerformanceListPage.value = data.data;
	      PerformanceRuleList.value = data.data.records;
	  })
	} else {
	  PageForm.value.page = value.page;
	  PageForm.value.page_size = value.page_size;
	  search(PageForm.value).then((data) => {
	     PerformanceListPage.value = data.data;
	     PerformanceRuleList.value = data.data.records;
	  });
	}
	
	
}


const importUrl = ref('/positionPerformance/upload');
const upload = ref({
  open: false,
  upLoadTitle: "员工关联部门业绩上传",
});


const onSuccess = (res) => {
    getRulePage(value).then(data=>{
        PerformanceListPage.value = data.data;
        PerformanceRuleList.value = data.data.records;
    })
    upload.value.open = false;
	
};
const handleUP = () => {
  upload.value.open = true;
};
const onDownTemplate = () => {
  getEmpPerformanceTemplate()
};

const title = ref("新增提成条目");
const type = ref("add");
const selectContent = ref([]);
const dialogFormVisible = ref(false);
const handleAdd = () => {
  selectContent.value = {
    id: "",
    name: "",
    target1: "",
    target2: "",
    type:"",
    state: "",
    ins: "",
  };
  title.value = "新增部门业绩条目";
  type.value = "add";
  dialogFormVisible.value = true;
};
const handleEdit = (index, row) => {
  selectContent.value = {
    id: "",
    name: "",
    target1: "",
    target2: "",
    state: true,
    ins: "",
  };
  selectContent.value = row;
  title.value = "编辑提成条目";
  type.value = "edit";
  dialogFormVisible.value = true;
};


const handleDelete = (index, row) => {
  deletetRule(row).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "删除成功",
        type: "success",
      });
      update();
    } else {
      ElMessage.error("删除失败");
    }
  });
};

const dialog = ref(false);
const handleCopy=()=>{
  dialog.value = true;
}
const closeForm = () => {
  dialog.value = false;
};

const close = () => {
  dialogFormVisible.value = false;
};

const update = (form) => {
	
	PageForm.value.deptName="",
	PageForm.value.positionName="",
	PageForm.value.target="",
	
  getRulePage(PageForm.value).then((data) => {
	  
    if (data.code == 200) {
      ElMessage({
        message: "更新成功",
        type: "success",
      });
      PerformanceListPage.value = data.data;
      PerformanceRuleList.value = data.data.records;
    } else {
      ElMessage.error("更新失败");
    }
  });
};

const compareSingleAttrReturnColSpan = (row, compareAttr, index, tableData) => {
  let count = 0;
  let start = index;
  let multCompare = true;

  while (start < tableData.length && multCompare) {
    start++;
    count++;
    //multCompare = row[compareAttr] === tableData[start]?.[compareAttr];//依旧符合相等条件
    //console.log(row.empId,tableData[start].empId);
    if (start <= tableData.length - 1)
      multCompare = row.id === tableData[start].id;
    else multCompare = row.id === tableData[start - 1].id;
  }
  start--; //退出循环后的 新起始行 index

  if (start < tableData.length) {
    tableData[index][`${compareAttr}_count`] = count; //将合并的行数存储在行数据中的一个特定属性中
  }

  if (index > 0 && count + 1 === tableData[index - 1][`${compareAttr}_count`]) {
    return [0, 0]; //不需要合并
  }
  return [count, 1];
};

const columns = [
  "name",
  "target1",
  "target2",
  "state",
  "updateTime",
  "ins",
  "operate",
];

const rowSpanRulesHandler = ({ row, column, rowIndex, columnIndex }, data) => {
  if (
    columns[columnIndex] === "name" ||
    columns[columnIndex] === "ins" ||
    columns[columnIndex] === "operate"
  ) {
    const r = compareSingleAttrReturnColSpan(row, "emp_id", rowIndex, data);
    return r;
  }
};


const handleDown = (index, row) => {
  downLoadPositionPerformance().then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "导出成功",
        type: "success",
      });
      update();
    } else {
      ElMessage.error("导出失败");
    }
  });
};


</script>

<style lang="scss" scoped>
.title1 {
  color: rgb(64, 158, 255);
  width: 300px;
  height: 45px;
  font-size: 25px;
  background: rgba(64, 158, 255, 0.4);
  text-align: center;
  border-radius: 10px;
  margin: 10px auto 10px;
  line-height: 45px;
  border-top: #409eff 2px solid;
  border-bottom: #409eff 2px solid;
  box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);
}
.pagination {
  margin: 20px 50px;
  display: flex;
  justify-content: end;
}
.el-table__body td {
     border: 2px solid black; /* 自定义表格内线条颜色 */
}
.opinion {
  width: 800px;
  height: 100%;
  border-radius: 8px;
  color: #363636;
  display: inline-block;
  border: #FFB400 2px dashed;
  background: #fff;
  margin: 10px 20px;
  padding: 10px;
}
</style>